package org.elcom.source;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ParseCDRFile {

	
    public static void parseFile(String parentDir){
        try {
            File[] subFile = new File(parentDir).listFiles();
            if(subFile != null && subFile.length >0){

                for(File aFile: subFile){//scan all file and folder
                    if(aFile.isDirectory()){ //skip Directory
                        parentDir = aFile.getAbsolutePath();
                        parseFile(parentDir);
                    }
                    else{
//                    	System.out.println("Doc file " + aFile.getAbsolutePath());
                        readFile(aFile.getAbsolutePath());
                    }
                }
            }
        } catch (Exception e){
        	e.printStackTrace();
            System.out.println("Error 2");
            
//            retryInsert();
        }
    }

    public static void readFile(String filePath) throws SQLException, IOException {
    	boolean isSuccess = true;

    	PreparedStatement stmt = null;

    	Connect.connectToDB();
        String insert = InsertIntoTable.sqlInsertToTable();
        stmt = Connect.connect.prepareStatement(insert);
        
        InsertIntoTable.executeInsert(filePath, stmt);
        
        if(isSuccess){
        	System.out.println("insert file " + new File(filePath).getAbsolutePath() + " success");
        }else {
			System.out.println("insert file " + new File(filePath).getAbsolutePath() + " failed");
		}
        InsertIntoTable.moveFileToOk(filePath);
    }
}
 